//商品列表
var app = getApp(); //全局APP

Page({
	//页面的初始数据
	data: {
		plan_class_id: 0,
		listData: [],
		showData: [],
		teacherList: [],
		showTeacherList: [],
		showTeacherId: [],
		showTeacherIndex: 0,
		currentXh: '',
		showChangeTeacherBtn: false,
		filterDlg: false
	},

	//加载执行
	onLoad: function(options) {
		var plan_class_id = options.plan_class_id;
		this.setData({
			plan_class_id: plan_class_id
		});
		
		this.getBookAplyDetail();
	},
	
	//加载执行
	onShow: function() {
	},
	
	getBookAplyDetail: function() {
		var page = this;
		var data = {
			plan_class_id: page.data.plan_class_id
		};
		
		// 获取班级
		app.api.getBookAplyDetail(data, function(res) {
			if (res.status) {
				var tmp_data = [];
				
				for (var i = 0; i < res.data.length; i++) {
					if(res.data[i].sdzt == '0') {
						res.data[i].sdzt = false;
					} else {
						res.data[i].sdzt = true;
					} 
				}
				
				page.setData({
					listData: res.data,
					showData: res.data,
					teacherList: res.teacherList,
					showChangeTeacherBtn: res.teacherList.length > 1
				});
			} else {
				//报错了
				app.common.errorToBack(res.msg, 0);
				page.setData({
					listData: [],
					showData: [],
					teacherList: [],
					showChangeTeacherBtn: false
				});
			}
		});
	},
	
	lock: function(e) {
		var page = this;
		var xh = e.currentTarget.dataset.xh;
		var data = {
			xh: xh
		};
		
		// 锁定
		app.api.studentlock(data, function(res) {
			if (res.status) {
				var listData = page.data.listData;
				var showData = page.data.showData;
				
				for (var i = 0; i < listData.length; i++) {
					if (listData[i].xh == xh) {
						listData[i].sdzt = true;
					}
				}
				
				for (var i = 0; i < showData.length; i++) {
					if (showData[i].xh == xh) {
						showData[i].sdzt = true;
					}
				}
				
				page.setData({
					listData: listData,
					showData: showData,
				});
			}
			
			app.common.errorToBack(res.msg, 0);
		});
	},
	
	unlock: function(e) {
		var page = this;
		var xh = e.currentTarget.dataset.xh;
		var data = {
			xh: xh
		};
		
		// 解锁
		app.api.studentunlock(data, function(res) {
			if (res.status) {
				var listData = page.data.listData;
				var showData = page.data.showData;
				
				for (var i = 0; i < listData.length; i++) {
					if (listData[i].xh == xh) {
						listData[i].sdzt = false;
					}
				}
				
				for (var i = 0; i < showData.length; i++) {
					if (showData[i].xh == xh) {
						showData[i].sdzt = false;
					}
				}
				
				page.setData({
					listData: listData,
					showData: showData,
				});
			}
			
			app.common.errorToBack(res.msg, 0);
		});
	},
	
	inputTyping: function(e) {
		if (e.detail.value !== '') {
			var actionArr = this.data.listData;
			var val = e.detail.value;
			var temp = []; 
			
			actionArr.forEach(function(i) {
				var xm = i.xm;
				var xh = i.xh;
				if (xm.indexOf(val) != -1 || xh.indexOf(val) != -1) {
					temp.push(i);
				}
			});
			
			this.setData({
				showData: temp,
			})
		} else {
			this.setData({
				showData: this.data.listData,
			})
		}
	},
	
	// 隐藏搜索框样式
	hideInput: function() {
		this.setData({
			inputVal: "",
			inputShowed: false,
			showData: this.data.listData
		});
	},

	changeTeacherShow: function(e) {
		// 筛选辅导员
		var tmp_Name = [];
		var tmp_id = [];
		var index = 0;
		for (var i = 0; i < this.data.teacherList.length; i++) {
			if(this.data.teacherList[i].truename != e.currentTarget.dataset.teacher) {
				tmp_Name[index] = this.data.teacherList[i].truename;
				tmp_id[index] = this.data.teacherList[i].id;
				index++;
			}
		}
		
		this.setData({
			currentXh: e.currentTarget.dataset.xh,
			showTeacherList: tmp_Name,
			showTeacherId: tmp_id
		})
		this.filter(e);
	},
	
	// 打开筛选窗口
	filter: function(e) {
		this.setData({
			filterDlg: true
		});
	},

	// 关闭筛选窗口
	close: function() {
		this.setData({
			filterDlg: false
		});
	},

	// 筛选提交
	filterSubmit: function() {
		var page = this;
			
		var data = {
			xh: page.data.currentXh,
			teacherId: page.data.showTeacherId[page.data.showTeacherIndex],
		};

		page.changeTeacher(data);
		 
		this.setData({
			filterDlg: false
		});
	},
 
	changeTeacher: function(data) {
		var page = this;
		
		app.api.changeTeacher(data, function(res) {
			if (res.status) {
				app.common.successToShow(res.msg, function() {
					page.getBookAplyDetail();
				});
			} else {
				app.common.errorToBack(res.msg, 0);
			}
		});
	},
	 
	// 绑定辅导员
	bindTeacherSelect: function(e) {
		var page = this;
		page.setData({
			teacherIndex: e.detail.value
		}); 
	},
});
